package org.dromara.cms.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.cms.domain.bo.CmsModelFieldBo;
import org.dromara.cms.domain.vo.CmsModelFieldVo;
import org.dromara.cms.domain.CmsModelField;
import org.dromara.cms.mapper.CmsModelFieldMapper;
import org.dromara.cms.service.ICmsModelFieldService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * CMS扩展字段Service业务层处理
 *
 * @author Spring
 * @date 2024-12-24
 */
@RequiredArgsConstructor
@Service
public class CmsModelFieldServiceImpl implements ICmsModelFieldService {

    private final CmsModelFieldMapper baseMapper;

    /**
     * 查询CMS扩展字段
     *
     * @param id 主键
     * @return CMS扩展字段
     */
    @Override
    public CmsModelFieldVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询CMS扩展字段列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return CMS扩展字段分页列表
     */
    @Override
    public TableDataInfo<CmsModelFieldVo> queryPageList(CmsModelFieldBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmsModelField> lqw = buildQueryWrapper(bo);
        Page<CmsModelFieldVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的CMS扩展字段列表
     *
     * @param bo 查询条件
     * @return CMS扩展字段列表
     */
    @Override
    public List<CmsModelFieldVo> queryList(CmsModelFieldBo bo) {
        LambdaQueryWrapper<CmsModelField> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmsModelField> buildQueryWrapper(CmsModelFieldBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmsModelField> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFieldName()), CmsModelField::getFieldName, bo.getFieldName());
        lqw.eq(StringUtils.isNotBlank(bo.getFieldKey()), CmsModelField::getFieldKey, bo.getFieldKey());
        lqw.eq(StringUtils.isNotBlank(bo.getFieldType()), CmsModelField::getFieldType, bo.getFieldType());
        lqw.eq(StringUtils.isNotBlank(bo.getDefaultValue()), CmsModelField::getDefaultValue, bo.getDefaultValue());
        lqw.eq(bo.getRequired() != null, CmsModelField::getRequired, bo.getRequired());
        lqw.eq(StringUtils.isNotBlank(bo.getModelType()), CmsModelField::getModelType, bo.getModelType());
        lqw.eq(bo.getSort() != null, CmsModelField::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getPlaceholder()), CmsModelField::getPlaceholder, bo.getPlaceholder());
        lqw.eq(StringUtils.isNotBlank(bo.getValidateRule()), CmsModelField::getValidateRule, bo.getValidateRule());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), CmsModelField::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增CMS扩展字段
     *
     * @param bo CMS扩展字段
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CmsModelFieldBo bo) {
        CmsModelField add = MapstructUtils.convert(bo, CmsModelField.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改CMS扩展字段
     *
     * @param bo CMS扩展字段
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CmsModelFieldBo bo) {
        CmsModelField update = MapstructUtils.convert(bo, CmsModelField.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmsModelField entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除CMS扩展字段信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
